<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\queue\connector\Database;

class Mobile extends Controller
{
    // 查询维修列表
    public function fixlist()
    {
        $data = $this->request->param(true);
        $more_sn = more_sn($data['sn']);
        //halt($more_sn);
        if ($more_sn['status'] == 0) {
            $res = Db::name('fix')->where('tagslogs_sn', $data['sn'])->find();
            $good_name = self::getGoodName($data['sn']);

            if ($res) {
                $temp = Db::name('fix_details')->where('pid', $res['id'])->select();
                $details = [];
                foreach ($temp as $key => $value) {
                    $details[$key]['id'] = $value['id'];
                    $details[$key]['project'] = $value['project'];
                    $details[$key]['personnel'] = $value['personnel'];
                    $details[$key]['telphone'] = $value['telphone'];
                    $details[$key]['date'] = date('Y-m-d', $value['date']);
                }
                return json(['status' => 1, 'msg' => '查询成功', 'good_name' => $good_name, 'list' => $details]);
            } else {
                return json(['status' => 0, 'msg' => '没有维修列表', 'good_name' => $good_name]);
            }
        } else {
            // 查询序列号
            $pid_arr = [];
            // 根据sn获取fix
            $fix1 = Db::name('fix')->where('tagslogs_sn', 'in', $more_sn['sn'])->select();
            $pid_arr = array_column($fix1, 'id');
            $id = Db::name('fix')->where('tagslogs_sn', $data['sn'])->value('id');
            array_push($pid_arr, $id);

            //$res = Db::name('fix')->where('tagslogs_sn', $data['sn'])->find();
            $good_name = self::getGoodName($data['sn']);

            $details = [];

            $temp = Db::name('fix_details')->where('pid', 'in', $pid_arr)->select();

            foreach ($temp as $key => $value) {
                $details[$key]['id'] = $value['id'];
                $details[$key]['project'] = $value['project'];
                $details[$key]['personnel'] = $value['personnel'];
                $details[$key]['telphone'] = $value['telphone'];
                $details[$key]['date'] = date('Y-m-d', $value['date']);
            }

            if ($details == []) {

                return json(['status' => 0, 'msg' => '没有维修列表', 'good_name' => $good_name]);
            } else {

                return json(['status' => 1, 'msg' => '查询成功', 'good_name' => $good_name, 'list' => $details]);
            }
        }
    }

    // 获取维修详情
    public function fixDetails($id)
    {
        $details = Db::name('fix_details')->where('id', $id)->find();
        $details['date'] = date('Y-m-d', $details['date']);
        $details['sn'] = Db::name('fix')->where('id', $details['pid'])->find()['tagslogs_sn'];
        $details['good_name'] = self::getGoodName($details['sn']);
        return json(['status' => 0, 'msg' => '查询成功', 'info' => $details]);
    }

    // 添加维修
    public function fixAdd()
    {
        $data = $this->request->param();
        //先写维修表
        $temp = Db::name('fix')->where('tagslogs_sn', $data['tagslogs_sn'])->find();
        if ($temp) {
            $pid = $temp['id'];
            Db::name('fix')->where('id', $pid)->setInc('fixnum');
        } else {
            $goodstags_id = Db::name('tagslogs')->where('sn', $data['tagslogs_sn'])->find()['goodstags_id'];
            if ($goodstags_id) {
                $good_id = Db::name('goodstags')->where('id', $goodstags_id)->find()['good_id'];
                $fix['good_id'] = $good_id;
                $fix['tagslogs_sn'] = $data['tagslogs_sn'];
                $pid = Db::name('fix')->insertGetId($fix);
                return json(['status' => 1, 'msg' => '添加成功']);
            } else {
                return json(['status' => 0, 'msg' => 'sn不存在']);
            }
        }

        //写详情表
        unset($data['tagslogs_sn']);
        $data['pid'] = $pid;
        $res = Db::name('fix_details')->insert($data);
        if ($res) {
            return json(['status' => 1, 'msg' => '添加成功']);
        } else {
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }

    // 获取sn对应的商品名称和关联的sn
    public function get_namebysn()
    {
        $data = $this->request->param();
        $good_id = Db::name('fix')->where('tagslogs_sn', $data['sn'])->find()['good_id'];
        $good_name = Db::name('good')->where('name', $good_id)->find()['name'];
        $temp = more_sn($data['sn']);
        return json(['status' => 1, 'msg' => '查询成功', 'data' => ['status' => $temp['status'], 'key' => $temp['key'], 'sn' => $temp['sn']]]);
    }

    // 获取保修状态
    public function get_fix_status()
    {
        $data = $this->request->param();
        $fix_status = Db::name('system')->find()['fix_status'];
        return json(['status' => $fix_status, 'msg' => '查询成功']);
    }

    // 获取保修卡详情
    public function get_card_desc()
    {
        $data = $this->request->param();
        $card_desc = Db::name('system')->where('id', 1)->value('card_desc');
        return json(['status' => 1, 'msg' => '查询成功', 'data' => $card_desc]);
    }


    // 添加维修
    public function afixAdd()
    {
        $data = $this->request->param();
        $post = $data;
        //先写维修表
        $temp = Db::name('fix')->where('tagslogs_sn', $data['tagslogs_sn'])->find();
        if ($temp) {
            $pid = $temp['id'];
            Db::name('fix')->where('id', $pid)->setInc('fixnum');
        } else {
            $goodstags_id = Db::name('tagslogs')->where('sn', $data['tagslogs_sn'])->find()['goodstags_id'];
            if ($goodstags_id) {
                $good_id = Db::name('goodstags')->where('id', $goodstags_id)->find()['good_id'];
                $fix['good_id'] = $good_id;
                $fix['tagslogs_sn'] = $data['tagslogs_sn'];
                $pid = Db::name('fix')->insertGetId($fix);
                // return json(['status' => 1, 'msg' => '添加成功']);
            } else {
                return json(['status' => 0, 'msg' => 'sn不存在']);
            }
        }

        //写详情表
        unset($data['tagslogs_sn']);
        unset($data['img']);
        $data['pid'] = $pid;
        $data['date'] = strtotime($data['date'] . '00:00:00');
        $data['where'] = 2;
        $data['flag'] = 1;
        $res = Db::name('fix_details')->insertGetId($data);
        //写图片表
        if (!empty($post['img'])) {
            foreach ($post['img'] as $key => $val) {
                if (!empty($val)) {
                    $insert = [
                        'imgurl' => $val,
                        'addtime' => time(),
                        'updatetime' => time(),
                        'pid' => $res,
                    ];
                    Db::name('fix_img')->insert($insert);
                }
            }
        }
        if ($res) {
            // 发送审核短信到用户
            $msg_temp = get_msg_temp(6);
            if ($msg_temp['is_on'] == 1) {
                if ($msg_temp['type'] == 1 || $msg_temp['type'] == 2) { //通知短信
                    send_mobile_message($data['telphone'], "useraddfix", $post['tagslogs_sn']);
                }
                if ($msg_temp['type'] == 1 || $msg_temp['type'] == 3) { //通知邮箱
                    if (!empty($data['email'])) {
                        //sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
                        sendToMail($data['email'], $msg_temp['email_title'], $msg_temp['email_content']);
                    }
                }
            }
            // 查询所有维修人员
            $kfs = Db::name('admin')->where(['status' => 1, 'is_wx' => 1])->select();
            $msg_temp1 = get_msg_temp(7);
            foreach ($kfs as $key => $value) {
                // 给客服人员发短信提醒
                if ($msg_temp1['is_on'] == 1) {
                    if ($msg_temp1['type'] == 1 || $msg_temp1['type'] == 2) { //通知短信
                        if (!empty($value['telephone'])) {
                            send_mobile_message($value['telephone'], "kfaddfix", $post['tagslogs_sn']);
                        }
                    }
                    if ($msg_temp1['type'] == 1 || $msg_temp1['type'] == 3) { //通知邮箱
                        if (!empty($value['email'])) {
                            //sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
                            sendToMail($value['email'], $msg_temp1['email_title'], $msg_temp1['email_content']);
                        }
                    }
                }
            }
            return json(['status' => 1, 'msg' => '添加成功', 'info' => $res]);
        } else {
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }

    // 得到商品名称
    private function getGoodName($sn)
    {
        $goodstags_id = Db::name('tagslogs')->where('sn', $sn)->find()['goodstags_id'];
        $good_id = Db::name('goodstags')->where('id', $goodstags_id)->find()['good_id'];
        return Db::name('good')->where('id', $good_id)->find()['name'];
    }

    /**
     * 获取设备激活状态
     */
    public function getActive()
    {
        $data = $this->request->param(true);
        $res = Db::name('card')->where('tagslogs_sn', $data['sn'])->find();
        $info = 0;
        if ($res) {
            if ($res['checkstatus'] == 1 && $res['endtime'] > time()) {
                $info = 1;
            } else if ($res['checkstatus'] == 0) {
                $info = 2;
            } else {
                $info = 3;
            }
            return json(['status' => 1, 'msg' => '查询成功', 'info' => $info]);
        } else {
            $more_sn = more_sn($data['sn']);
            if ($more_sn['status'] == 1) {
                $res = Db::name('card')->where('tagslogs_sn', $more_sn['sn'])->find();
                $info = 0;
                if ($res) {
                    if ($res['checkstatus'] == 1 && $res['endtime'] > time()) {
                        $info = 1;
                    } else if ($res['checkstatus'] == 0) {
                        $info = 2;
                    } else {
                        $info = 3;
                    }
                    return json(['status' => 1, 'msg' => '查询成功', 'info' => $info]);
                } else {

                    return json(['status' => 0, 'msg' => '查询失败', 'info' => $info]);
                }
            } else {
                return json(['status' => 0, 'msg' => '查询失败', 'info' => $info]);
            }
        }
    }

    /**
     * 设备保修状态
     */
    public function getActiveStatus()
    {
        $data = $this->request->param(true);
        $goodstags_id = Db::name('tagslogs')->where('sn', $data['sn'])->find()['goodstags_id'];
        if (!$goodstags_id) {
            return ['status' => 0, 'msg' => 'sn不存在'];
        }
        $good_id = Db::name('goodstags')->where('id', $goodstags_id)->find()['good_id'];
        $res = Db::name('card')->where('tagslogs_sn', $data['sn'])->find();
        $info['name'] = Db::name('good')->where('id', $good_id)->find()['name'];
        $info['sn'] = $data['sn'];
        $info['addtime'] = $res['addtime'] == 0 ? null : date('Y-m-d', $res['addtime']);
        $info['user'] = $res['user'];
        $info['com'] = $res['danwei'];
        $info['djs'] = 0;
        $info['hm'] = 0;
        if ($res['endtime'] > time()) {
            $info['djs'] = 1;
            $info['hm'] = ceil(($res['endtime'] - time()) / 86400);
            //  $info['hm']= $res['endtime'] - time();
        }
        $info['endtime'] = $res['endtime'] == 0 ? null : date('Y-m-d', $res['starttime']) . ' 至 ' . date('Y-m-d', $res['endtime']);

        return json(['status' => 1, 'msg' => '查询成功', 'info' => $info]);
    }

    /**
     * 获取维修图片
     */
    public function getFixImgs()
    {
        $data = $this->request->param(true);
        $imgs = Db::name('fix_img')->where('pid', $data['id'])->select();
        if ($imgs) {
            return json(['status' => 1, 'msg' => '查询成功', 'info' => array_column($imgs, 'imgurl')]);
        } else {
            return json(['status' => 0, 'msg' => '查询失败']);
        }
    }

    /**
     * 设备保修卡
     */
    public function getCard()
    {
        $data = $this->request->param(true);
        $res = Db::name('card')->where('tagslogs_sn', $data['sn'])->find();
        $info['bianhao'] = $res['bianhao'];
        $info['com'] = $res['danwei'];
        $info['user'] = $res['user'];
        $info['phone'] = $res['phone'];
        $info['mobile_phone'] = $res['mobile_phone'];
        $info['email'] = $res['email'];

        $goodstags_id = Db::name('tagslogs')->where('sn', $data['sn'])->find()['goodstags_id'];
        $good_id = Db::name('goodstags')->where('id', $goodstags_id)->find()['good_id'];
        $info['model'] = Db::name('good')->where('id', $good_id)->find()['model'];
        $info['sn'] = $data['sn'];
        $info['endtime'] = $res['endtime'];
        $info['addtime'] = $res['addtime'];

        if ($info['endtime'] == 0) {
            $info['endtime'] = '暂未激活';
        } else if ($info['endtime'] > time()) {
            $info['endtime'] = date('Y-m-d', $info['addtime']) . ' 至 ' . date('Y-m-d', $info['endtime']);
        } else {
            $info['endtime'] = '已过期';
        }

        return json(['status' => 1, 'msg' => '查询成功', 'info' => $info]);
    }

    /**
     * 根据sn获取商品名称
     */
    public function getNameBySn()
    {
        $data = $this->request->param();
        // 查询sn对应的good_id
        $tagslogs = Db::name('tagslogs')->where('sn', $data['sn'])->find();
        if (empty($tagslogs)) {
            return json(['status' => 0, 'msg' => '查询结果为空', 'info' => '']);
        }
        // 根据goodstags_id获取商品ID
        $goodstags = Db::name('goodstags')->where('id', $tagslogs['goodstags_id'])->find();
        if (empty($goodstags)) {
            return json(['status' => 0, 'msg' => '查询结果为空', 'info' => '']);
        }
        // 根据good_id查看商品名称
        $good = Db::name('good')->where('id', $goodstags['good_id'])->find();
        if (empty($good)) {
            return json(['status' => 0, 'msg' => '查询结果为空', 'info' => '']);
        }
        return json(['status' => 1, 'msg' => '查询成功', 'info' => $good['name']]);
    }

    // 查询广告列表
    public function getBannerList()
    {
        $list = Db::name('banner')->field('img,sort')->order('sort asc')->select();
        $status = 0;
        $msg = '数据为空';
        if (!empty($list)) {
            $status = 1;
            $msg = '查询成功';
        }
        return json(['status' => $status, 'msg' => $msg, 'data' => $list]);
    }

    // 查询资料数据
    public function getSeriesData()
    {
        $data = $this->request->param();
        $sql = Db::name('series_data');
        if (isset($data['search']) && !empty($data['search'])) {
            $sql = $sql->where('name', 'like', '%' . trim($data['search']) . '%');
        }
        $goodstags_id = Db::name('tagslogs')->where('sn', $data['sn'])->value('goodstags_id');
        $good_id = Db::name('goodstags')->where('id', $goodstags_id)->value('good_id');
        $series_id = Db::name('good')->where('id', $good_id)->value('series');
        $sql = $sql->where('pid', $series_id);
        $list = $sql->select();
        foreach ($list as $key => $value) {
            if ($value['ext'] == 'jpg' || $value['ext'] == 'jpeg' || $value['ext'] == 'gif' || $value['ext'] == 'bmp' || $value['ext'] == 'png') {
                $list[$key]['type'] = 'img';
            } else if ($value['ext'] == 'mp4' || $value['ext'] == 'ogg' || $value['ext'] == 'webm') {
                $list[$key]['type'] = 'video';
            } else if ($value['ext'] == 'txt') {
                $list[$key]['type'] = 'txt';
            } else if ($value['ext'] == 'pdf') {
                $list[$key]['type'] = 'pdf';
            } else if ($value['ext'] == 'zip' || $value['ext'] == 'rar' || $value['ext'] == '7z' || $value['ext'] == 'tar') {
                $list[$key]['type'] = 'zip';
            } else {
                $list[$key]['type'] = 'other';
            }

            $list[$key]['url'] = '/uploads/series_data/' . $value['url'];
        }
        return json(['status' => 1, 'msg' => '查询成功', 'list' => $list]);
    }

    // 写资料日志
    public function getSeriesDataLogs()
    {
        $data = $this->request->param();
        $data['addtime'] = time();
        $res = Db::name('series_data_logs')->insert($data);
        if (!$res) {
            return json(['status' => 0, 'msg' => '添加失败']);
        }
        return json(['status' => 1, 'msg' => '添加成功']);
    }
}
